//
//  RegisteredViewController.m
//  TravelFirst
//
//  Created by lanou3g on 15/9/22.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "RegisteredViewController.h"
#import "RegisteredView.h"
#import <CommonCrypto/CommonDigest.h>
#import "MyHandle.h"

@interface RegisteredViewController ()

@property (nonatomic,strong) RegisteredView *reg;

@end

@implementation RegisteredViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.reg = [[RegisteredView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    UIButton *but = (UIButton *)[_reg viewWithTag:500];
    [but addTarget:self action:@selector(regist:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_reg];
    
}

//post请求 传入url和拼接后的字符串
- (NSDictionary *)postData:(NSString *)urlString option:(NSString *)option{
    
    ////获取到网址
    NSURL *url = [NSURL URLWithString:urlString];
    //创建一个请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:15];
    //设置请求类型
    [request setHTTPMethod:@"Post"];
    
    //注意:  post 请求 和 Get 请求 就以下两部不一样  其他完全一样
    //创建一个NSData的参数  并转化为UTF-8的编码格式
    NSData *pramadata = [option dataUsingEncoding:NSUTF8StringEncoding];
    //携带参数 (可以携带多参数   不是特常见)
    [request setHTTPBody:pramadata];
    
    NSURLResponse *response = nil;
    NSError *error = nil;
    
    //创建链接 并转化为data数据
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    //解析数据
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
    return dic;
}

//邮箱正则表达式
- (BOOL) validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}
//密码正则
- (BOOL) validatePassword:(NSString *)passWord
{
    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:passWord];
}

- (NSString *)md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

//点击注册账号触发的方法
- (void)regist:(UIButton *)butt{
    UITextField *text1 = (UITextField *)[_reg viewWithTag:100];
    UITextField *text2 = (UITextField *)[_reg viewWithTag:101];
    UITextField *text3 = (UITextField *)[_reg viewWithTag:102];

    if([self validateEmail:text1.text]){
        if([self validatePassword:text2.text]){
            if([text3.text isEqual:nil]){
            
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
